package com.controller;

import com.vo.ResponseBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@CrossOrigin("*")
public class UploadController {



    @Value("${upload.dir}")
    private String tmp;


    @RequestMapping(value = "/upload")
    public ResponseBean upload(MultipartFile file)throws IOException{

        //根据时间随机产生新的文件名
        String oldFileName=file.getOriginalFilename(); //原来的文件名 a.png
        int index=oldFileName.lastIndexOf(".");  //最后一个点的位置
        String extName=oldFileName.substring(index); //从点开始截取到最后
        String newFileName=System.currentTimeMillis()+extName;
        //传输
        File desc=new File(tmp,newFileName);
        file.transferTo(desc);
        return new ResponseBean(0,0,newFileName);

    }

}
